#!/bin/sh
# use @DEFAULT_SINK@ on linux
# check the output of mixerctl(1) on NetBSD

value=5
if test "$3" != ""; then
  value=$3
fi

os=`uname`

if test "$os" = "NetBSD"; then
  channel="$1"
  if test "$2" = "mute"; then
   channel="$channel.mute"
  fi
  channels=`mixerctl $channel | cut -d "=" -f 2`
  volume=`echo $channels | cut -d "," -f 1`
  newvol=$volume

  case $2 in
    "increase")
      newvol=$(($volume+value))
      ;;
    "decrease")
      newvol=$(($volume-value))
      ;;
    "mute")
      if test "$newvol" = "off"; then
        newvol="on"
      else
        newvol="off"
      fi
      ;;
    *)
      echo "Unsupport argument. Use either increase or decrease."
      exit 1
      ;;
  esac

  echo $newvol

  if [ $newvol != $volume ]
  then
      mixerctl -w  $channel=$newvol
      newval=`mixerctl  $channel | cut -d "=" -f 2`
      pkill -SIGUSR1 herbe
      # this will output off for when mute is off whic his confusing
      # because you'd want to see if the volume is on or not.
      if test "$newval" = "on"; then
        newval="off"
      elif test "$newval" = "off"; then
        newval="on"
      fi
      herbe "Volume ${newval}" &
  fi
else
  case $2 in
    "increase")
      wpctl set-volume $1 $value%+
      ;;
    "decrease")
      wpctl set-volume $1 $value%-
      ;;
    "mute")
      wpctl set-mute $1 toggle
      ;;
    *)
      ;;
  esac

  vol=`wpctl get-volume $1`

  notify-send -u "low" "$vol"
fi
